package org.qs.webservice.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.qs.webservice.dao.content.ConBookMapper;
import org.qs.webservice.dao.user.UsBookMarkMapper;
import org.qs.webservice.domain.ConBook;
import org.qs.webservice.domain.UsBookMark;
import org.qs.webservice.service.BookMarkService;
import org.qs.webservice.service.IreadMemcacheService;
import org.qs.webservice.util.DateUtil;
import org.qs.webservice.util.IReadUtils;
import org.qs.webservice.util.IdWorker;
import org.qs.webservice.util.StringUtils;
import org.qs.webservice.vo.Content;
import org.qs.webservice.vo.PageData;
import org.springframework.stereotype.Service;

@Service
public class BookMarkServiceImpl implements BookMarkService {
	@Resource
	private IreadMemcacheService ireadMemcacheService;
	@Resource
	private ConBookMapper conBookMapper;
	@Resource
	private UsBookMarkMapper usBookMarkMapper;
	@Override
	public String addBookMark(int book_id, int chap_id, int chapter_offset,
			String token, String device_identifier) {
		Map<String,Object> tokenMap = new HashMap<String,Object>();
		if(book_id<=0||chap_id<=0){
			tokenMap.put("code", 403);
			tokenMap.put("reason","参数错误");
			return IReadUtils.beanToJson(tokenMap);
		}
		String user_id  = IReadUtils.getUserIdByToken(token, ireadMemcacheService, device_identifier);
		if(StringUtils.isNullOrEmpty(user_id)){
			tokenMap.put("code", 403);
			tokenMap.put("reason","用户未登录");
			return IReadUtils.beanToJson(tokenMap);
		}
		ConBook conBook = conBookMapper.getBookById(book_id);
		if(null==conBook){
			tokenMap.put("code", 403);
			tokenMap.put("reason","书籍已下架");
			return IReadUtils.beanToJson(tokenMap);
		}
		UsBookMark usBookMark = new UsBookMark();
		usBookMark.setBook_id(book_id);
		usBookMark.setChapter_id(chap_id);
		usBookMark.setUser_id(Long.valueOf(user_id));
		usBookMark.setChapter_offset(chapter_offset);
		IdWorker idWorker = new IdWorker(2);
		usBookMark.setMark_id(idWorker.nextId());
		usBookMarkMapper.addBookMark(usBookMark);
		tokenMap.put("code", 200);
		tokenMap.put("content","操作成功");
		return IReadUtils.beanToJson(tokenMap);
	}
	@Override
	public String deleteMark(List<String> mark_ids, String token, String device_identifier) {
		String user_id  = IReadUtils.getUserIdByToken(token, ireadMemcacheService, device_identifier);
		Map<String,Object> tokenMap = new HashMap<String,Object>();
		if(StringUtils.isNullOrEmpty(user_id)){
			tokenMap.put("code", 403);
			tokenMap.put("reason","用户未登录");
			return IReadUtils.beanToJson(tokenMap);
		}
		for(String mark_id:mark_ids){
			try {
				usBookMarkMapper.deleteBookMark(Long.valueOf(mark_id),Long.valueOf(user_id));
			} catch (NumberFormatException e) {
			}
		}
		tokenMap.put("code", 200);
		tokenMap.put("content","操作成功");
		return IReadUtils.beanToJson(tokenMap);
	}
	@Override
	public String getBookMarkList(int book_id, int pages, int contents,
			String token, String device_identifier) {
		Map<String,Object> tokenMap = new HashMap<String,Object>();
		if(book_id<0){
			book_id=0;
		}
		if(pages==0){
			pages=1;
		}
		if(contents==0){
			contents=20;
		}
		int index=(pages-1)*contents;
		String user_id  = IReadUtils.getUserIdByToken(token, ireadMemcacheService, device_identifier);
		if(StringUtils.isNullOrEmpty(user_id)){
			tokenMap.put("code", 403);
			tokenMap.put("reason","用户未登录");
			return IReadUtils.beanToJson(tokenMap);
		}
		if(book_id!=0){
			ConBook conBook = conBookMapper.getBookById(book_id);
			if(null==conBook){
				tokenMap.put("code", 403);
				tokenMap.put("reason","书籍已下架");
				return IReadUtils.beanToJson(tokenMap);
			}
		}
		List<Content> contentList = new ArrayList<Content>();
		
		List<UsBookMark> bookMarks = usBookMarkMapper.getBookMarks(book_id, Long.valueOf(user_id), index, contents);
		for(UsBookMark usBookMark :bookMarks){
			Content content = new Content();
			content.setMark_id(usBookMark.getMark_id()+"");
			content.setContent_id(usBookMark.getBook_id());
			content.setContent_name(usBookMark.getBook_name());
			if(usBookMark.getChapter_id()==0){
				content.setChapter_id(usBookMark.getFirst_chap_id());
			}else
				content.setChapter_id(usBookMark.getChapter_id());
			content.setChapter_name(usBookMark.getMark_name());
			
			content.setMark_time(DateUtil.getString(usBookMark.getMark_time(), "yyyy-MM-dd HH:mm:ss"));
			//content.setMark_content(usBookMark.getMark_content());
			contentList.add(content);
		}
		PageData<Content> pageData = new PageData<Content>();
		pageData.setCode(200);
		pageData.setContent(contentList);
		pageData.setPageNumber(pages);
		pageData.setPageSize(contents);
		pageData.setTotal(usBookMarkMapper.getBookMarksNum(book_id, Long.valueOf(user_id)));
		return IReadUtils.beanToJson(pageData);
	}
	
}
